#include "creategroup.h"
#include "ui_creategroup.h"
#include <QListWidgetItem>
#include <QMessageBox>


CreateGroup::CreateGroup(QString user, QWidget *parent) :
    QWidget(parent),
    ui(new Ui::CreateGroup), userName(user)
{
    ui->setupUi(this);
    ui->lw_users->setSelectionMode(QAbstractItemView::ExtendedSelection);
    this->setWindowTitle(userName + "的创建群聊");
}


CreateGroup::~CreateGroup()
{
    delete ui;
}

void CreateGroup::closeEvent(QCloseEvent *event)
{
    Q_UNUSED(event)
    this->deleteLater();
}

void CreateGroup::onAllUsers(QStringList users)
{
    for (auto user : users)
    {
        if (user == this->userName) continue;
        QListWidgetItem *item = new QListWidgetItem(user);
        item->setTextAlignment(Qt::AlignCenter);
        ui->lw_users->addItem(item);
    }
}

void CreateGroup::on_ptn_create_clicked()
{
    QString groupName = ui->le_groupName->text();
    if (groupName.isEmpty())
    {
        QMessageBox::warning(this, "创建", "组名不能为空");
        return ;
    }
    QList<QListWidgetItem*> selectedItems = ui->lw_users->selectedItems();
    QStringList users;
    users.push_back(userName);
    for (auto user : selectedItems)
    {
        users.push_back(user->text());
    }
    emit group(users, groupName);
    this->deleteLater();
}
